//
//  HYLanguageService.m
//  JFBase
//
//  Created by victor lau on 2018/12/10.
//  Copyright © 2018年 Alan Wang. All rights reserved.
//

#import "HYLanguageService.h"



@implementation HYLanguageService


//支持简体与英文
+ (void)initHYLanguageShowType
{
    //当app内没有设置时
    if ([[NSUserDefaults standardUserDefaults] objectForKey: AppLanguage] == nil)
    {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"system-language"];
    }
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"system-language"] == YES)
    {
        NSString *str = nil;
        NSString *currentLanguage = [[NSLocale preferredLanguages] objectAtIndex:0];
        if ([currentLanguage hasPrefix:@"zh-Hant"]
            || [currentLanguage hasPrefix:@"zh-Hant-CN"]
            || [currentLanguage hasPrefix:@"zh-Hant-MO"]
            || [currentLanguage hasPrefix:@"zh-Hant-HK"]
            || [currentLanguage hasPrefix:@"zh-Hant-TW"]
            || [currentLanguage hasPrefix:@"zh-Hant-SG"])
        {
            str = @"zh-Hant";   //繁体
        }
        else if ([currentLanguage hasPrefix:@"zh-Hans"]
                 || [currentLanguage hasPrefix:@"zh-Hans-CN"]
                 || [currentLanguage hasPrefix:@"zh-Hans-MO"]
                 || [currentLanguage hasPrefix:@"zh-Hans-HK"]
                 || [currentLanguage hasPrefix:@"zh-Hans-TW"]
                 || [currentLanguage hasPrefix:@"zh-Hans-SG"])
        {
            str = @"zh-Hans";  //简体
        }
        else if ([currentLanguage hasPrefix:@"en"])
        {
            str = @"en";
        }
        else   //其它情况显示英文
        {
            str = @"en";
        }
        [[NSUserDefaults standardUserDefaults] setObject:str forKey:AppLanguage];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}


//支持简体与英文
+ (HYLanguageType)getHYLanguageShowType
{
    NSString *typeStr = [[NSUserDefaults standardUserDefaults] objectForKey: AppLanguage];
    if ([typeStr isEqualToString:@"zh-Hans"])
    {
        return HYLanguageTypeChinese; //简体
    }
    else if ([typeStr isEqualToString:@"zh-Hant"])
    {
        return HYLanguageTypeHK; //繁体

    }
    else if ([typeStr isEqualToString:@"en"])
    {
        return HYLanguageTypeEnglish;
    }
    return HYLanguageTypeEnglish;
}



//注意：必须是客户手动设置语言类型时，才能调用此接口
+ (void)setHYLanguageShowType:(HYLanguageType)type
{
    NSString *str = nil;
    switch (type)
    {
        case HYLanguageTypeChinese:
        {
            str = @"zh-Hans";
        }
            break;
        case HYLanguageTypeHK:
        {
            str = @"zh-Hant";  //繁体
        }
            break;
        case HYLanguageTypeEnglish:
        {
            str = @"en";
        }
            break;
        default:
        {
            //            str = @"zh-Hans";
            str = @"en";
        }
            break;
    }
    
    [[NSUserDefaults standardUserDefaults] setObject:str forKey:AppLanguage];
    
    
    //改变语言的通知
    [[NSNotificationCenter defaultCenter] postNotificationName:KNotify_Language_Change object:nil];
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"system-language"] == YES)
    {
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"system-language"];
    }
    [[NSUserDefaults standardUserDefaults] synchronize];
}





+(HYCurrencyType)getHYCurrencyShowType
{
    NSString *typeStr = [[NSUserDefaults standardUserDefaults] objectForKey: KUserDefaultCurrency];
    if ([typeStr isEqualToString:@"USD"])
    {
        return HYCurrencyTypeUSD;
    }
    else if ([typeStr isEqualToString:@"CNY"])
    {
        return HYCurrencyTypeCNY;
    }else if([typeStr isEqualToString:@"HKD"])
    {
        return HYCurrencyTypeHKD;
    }
    
    return HYCurrencyTypeUSD;  //默认美元
}


+(void)saveCurrencyType:(HYCurrencyType)type
{
    NSString * str = @"USD";
    switch (type) {
        case HYCurrencyTypeUSD:
            str = @"USD";
            break;
        case HYCurrencyTypeCNY:
            str = @"CNY";
            break;
        case HYCurrencyTypeHKD:
            str = @"HKD";
            break;
        default:
            break;
    }
    
    //改变货币的通知
    [[NSNotificationCenter defaultCenter] postNotificationName:KNotify_Currency_Change object:nil];
    
    [[NSUserDefaults standardUserDefaults] setObject:str forKey:KUserDefaultCurrency];
    [[NSUserDefaults standardUserDefaults] synchronize];
}


+(NSString *)httpHeadLanguage
{
    NSString * language = @"zh_CN";
    if ([HYLanguageService getHYLanguageShowType] == HYLanguageTypeChinese) {
        language = @"zh_CN";
    }else if ([HYLanguageService getHYLanguageShowType] == HYLanguageTypeHK) {
        language = @"el_GR";
    }else  if ([HYLanguageService getHYLanguageShowType] == HYLanguageTypeEnglish) {
        language = @"en_US";
    }
    
    return language;
}




@end
